<?php
defined('IN_IA') or exit('Access Denied');
define('SERVERURL', 'http://iot.pouaka.com:20025');
define('bdkey', 'vittor_test_001_liuzhufeng_20180413184901');

require IA_ROOT. '/addons/vittor_homework_cabinet/core/common/defines.php';
require TG_CORE . 'class/wlloader.class.php';
wl_load()->func('global');
wl_load()->func('pdo');
wl_load()->func('tpl');
class vittor_homework_cabinetModuleSite extends WeModuleSite {
	public function __call($name, $arguments) {
		global $_W;
		$isWeb = stripos($name, 'doWeb') === 0;
		$isMobile = stripos($name, 'doMobile') === 0;
		if($isWeb || $isMobile) {
			$dir = IA_ROOT . '/addons/' . $this->modulename . '/';
			if($isWeb) {
				$dir .= 'web/';
				$controller = strtolower(substr($name, 5));
			}
			if($isMobile) {
				$dir .= 'app/';
				$controller = strtolower(substr($name, 8));
			}
			$file = $dir . 'index.php';
			if(file_exists($file)) {
				require $file;
				exit;
			}
		}
		trigger_error("访问的方法 {$name} 不存在.", E_USER_WARNING);
		return null;
	}
	public function doMobileHomecover(){
        global $_W;
		$dir = IA_ROOT . '/addons/' . $this->modulename . '/';
		$dir .= 'app/';
		$file = $dir . 'index.php';
		if(file_exists($file)) {
			require $file;
			exit;
		}		
		//print_r( $_W['openid']);
    }
    public function doMobileTransaction(){
        global $_W;
        $dir = IA_ROOT . '/addons/' . $this->modulename . '/';
        $dir .= 'app/';
        $file = $dir . 'index.php';
        if(file_exists($file)) {
        global $_GPC;
            $_GPC['do']='scangenerate';
            $_GPC['ac']='scan';
            $_GPC['op']='door_teacher';
            $_GPC['info_id']='36';
            $_GPC['door']='4';
            $_GPC['random']='pI03GDTBcH';
            $_GPC['device_name']='逸夫馆';
            require $file;
            exit;
        }
    }
	public static function postRequest($data='',$serverurl=SERVERURL,$content_type='text/plain') {
        $ch = curl_init($serverurl); 
        if(is_array($data)){
            $postdata = http_build_query($data);
        }else{
            $postdata=$data;
        }
        //print_r($postdata);
        curl_setopt($ch, CURLOPT_HTTP_VERSION, CURL_HTTP_VERSION_1_0);
        curl_setopt($ch, CURLOPT_POST, 1);
        curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
        curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, FALSE);
        curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");
        curl_setopt($ch, CURLOPT_POSTFIELDS, $postdata);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
        curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type:'.$content_type.';charset=utf-8'));
        $result=curl_exec($ch);
        curl_close($ch);
        return $result;
    }
	public function all_device_info($type){//用于web端下拉列表
        if($type==0)
            $request_data = array(
                'bdkey'=>bdkey,
                'platform'=>'opensource',
                'api'=>'getdeviceinfo',
                'type'=>$type
            );
        if(is_array($type))
            $request_data = array(
                'bdkey'=>bdkey,
                'platform'=>'opensource',
                'api'=>'getdeviceinfo',
                'type'=>$type
            ); 
        //print_r($request_data);
		$device=json_decode($this->postRequest($request_data),true);
		//print_r($device);
		$return_data=array();
        if($device['state']=='success')
            $return_data = $device['device_info'];
        //print_r($return_data);
		return $return_data;
	}
    public function return_random(){
        $str="QWERTYUIOPASDFGHJKLZXCVBNM1234567890qwertyuiopasdfghjklzxcvbnm";
        str_shuffle($str);
        $name=substr(str_shuffle($str),26,10);
        return $name;
    }
    public function check_in_weixin(){
        global $_W;
        //print_r($_W['openid']);
        // if ( strpos($_SERVER['HTTP_USER_AGENT'], 'MicroMessenger') == false ){
            // $prompt = '请在微信中打开链接！';
            // include wl_template('tips/prompt');
             // exit;             
        // }
        if($_W['openid']==null|$_W['openid']==''){
            $prompt = '亲，获取不到您的微信号，请再试一次！';
            include wl_template('tips/prompt');
             exit;            
        }
    }    
    public function get_id_to_name(){//获得信息ID与设备名称对应的数组
        global $_W;
        $to_find = array();
        $all = pdo_getall('homework_cabinet_admin', array('uniacid' => $_W['uniacid']) ,array('device_info_id','device_name') );
        
        foreach($all as $key=>$value){
            if($value['device_name']!=''){
                $to_find[$value['device_info_id']] = $value['device_name'];
            }
        }
       
        return $to_find;
    }     
    public function post_open_door($device_info_id,$door=0){
		$request_data = array(
			'bdkey'=>bdkey,
			'platform'=>'opensource',
			'api'=>'opendevice',
            'device_info_id'=>$device_info_id,
            'door_num'=>$door
		);
        $data=json_decode($this->postRequest($request_data),true);
        //这里的数据返回后还需要处理
        return $data;
    }

	public function doWebTest(){
		global $_W;
		print_r( $_W['openid']);
	}
	public function doMobileOperate(){
                        global $_W;
                        $openid = $_W['openid'];
                        //$openid ='ooVdA0itTwfImmGXQLyM6hj80Y_M';
                        $account_api = WeAccount::create();
                        $token = $account_api->getAccessToken();
                        if(is_error($token)){
                            return $this->respText('获取access token 失败');                           
                        }
                        $url = 'https://api.weixin.qq.com/cgi-bin/message/template/send?access_token='.$token;
                        $template_id = '4sT4hto1K27PMwQgodhSsgL_0cDCzrHYRJvd17dlUPg';
                        $templateData = array('first'=>array(
                                                        'value'=>'您收到了一条新的门店订单。',
                                                        'color'=>'#173177'),
                                                'info_fo'=>array(
                                                        'value'=>'2015年08月17日',
                                                        'color'=>'#173177'),
                                                'device_name'=>array(
                                                        'value'=>'未付款',
                                                        'color'=>'#173177'),
                                                'door'=>array(
                                                        'value'=>'李先生',
                                                        'color'=>'#173177'),
                                                'name'=>array(
                                                        'value'=>'预约服务',
                                                        'color'=>'#000'),
                                                'remark'=>array(
                                                        'value'=>'为了完美的服务体验，请提前安排好时间',
                                                        'color'=>'#173177'));
                        $data = array(
                            'touser'=>$openid,
                            'template_id'=>$template_id,
                            'data'=>$templateData
                        );                                
                        $re=json_decode($this->postRequest(json_encode($data) , $url),true);
                        print_r(json_encode($re));
                       
	}    
    public function doMobileManagement(){
        global $_W;
		print_r( $_W['openid']);
    }
	public function doMobileTest(){
		$request_data = array(
			'bdkey'=>bdkey,
			'platform'=>'opensource',
			'manage_type'=>'opendevice',
            'device_info_id'=>'36',
            'door_num'=>1
		);
        $data=json_decode($this->postRequest($request_data),true);
        print_r($data);
	}
}
